import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/api/home.dart';
import 'package:flutter_enjoy_plus/pages/home/components/home_list.dart';
import 'package:flutter_enjoy_plus/pages/home/components/home_nav.dart';
import 'package:flutter_enjoy_plus/utils/Toast.dart';
import 'package:flutter_enjoy_plus/utils/TokenManager.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  _IndexState createState() => _IndexState();
}

class _IndexState extends State<HomePage> {
  // 获取社区公告列表
  List announcementList = [];

  @override
  void initState() {
    super.initState();
    getAnnounceList();
  }

  // 获取公告列表方法
  getAnnounceList() async {
    final res = await getAnnounceListAPI();
    setState(() {
      announcementList = res;
    });
    // print(res);
    PromptAction.showToast('获取公告列表成功');
  }

  int countTime = 60;

  Timer? timer;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromRGBO(229, 231, 242, 1),
      appBar: AppBar(
        title: const Text('享+社区'),
        centerTitle: true,
        backgroundColor: Colors.transparent,
      ),
      body: ListView(
        padding: const EdgeInsets.all(10),
        children: [
          // 弹窗测试
          // ElevatedButton(onPressed: () {
          //   // 有选择的弹窗
          //   // showAboutDialog(context: context);

          //   // 日期选择器
          //   // showDatePicker(
          //   //   context: context,
          //   //   firstDate: DateTime(2000),
          //   //   lastDate: DateTime(2025),
          //   // );

          //   // 底部模态弹窗
          //   // showBottomSheet(context: context, builder: builder);

          // }, child: const Text('弹窗测试')),
          Row(
            children: [
              // Expanded(
              //     child: ElevatedButton(
              //   onPressed: () {
              //     tokenManager.setToken('123456789');
              //   },
              //   child: const Text('设置token'),
              // )),
              // Expanded(
              //     child: ElevatedButton(
              //   onPressed: () {
              //     tokenManager.deleteToken();
              //   },
              //   child: const Text('删除token'),
              // )),
              Expanded(
                  child: ElevatedButton(
                onPressed: () {
                  final res = tokenManager.getToken();
                  PromptAction.showToast('获取token为：$res');
                },
                child: const Text('获取token'),
              )),
              Expanded(
                  child: ElevatedButton(
                onPressed: () {
                  //  开启定时器逻辑
                  timer = Timer.periodic(const Duration(seconds: 1), (timer) {
                    setState(() {
                      countTime--;
                    });
                  });
                },
                child: const Text('开启定时器'),
              )),
              Text('剩余: $countTime秒'),
              Expanded(
                  child: ElevatedButton(
                onPressed: () {
                  //  关闭定时器逻辑
                  timer?.cancel();
                },
                child: const Text('关闭定时器'),
              )),
              Expanded(
                  child: ElevatedButton(
                onPressed: () {
                  //  延时器逻辑
                  Future.delayed(const Duration(seconds: 3), () {
                    PromptAction.showToast('延时器');
                  });
                },
                child: const Text('延时器'),
              ))
            ],
          ),

          // 头部导航
          HomeNav(),
          // 广告区
          ClipRRect(
              borderRadius: BorderRadius.circular(10),
              child: Image.asset('assets/images/banner@2x.jpg')),
          // 社区公告
          HomeList(
            notifyList: announcementList,
          )
        ],
      ),
    );
  }
}
